package com.leo;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.*;
import java.util.List;

/**
 * @ClassName FileChooserDemo
 * @Description
 * @Author wangss
 * @date 2020.06.01 21:59
 * @Version 1.0
 */
public class FileChooserDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        VBox vBox = new VBox(10);

        Button b1 = new Button("单选文件窗口");
        Button b2 = new Button("多选文件窗口");
        Button b3 = new Button("打开文本");
        Button b4 = new Button("保存文本");
        Button b5 = new Button("打开文件夹");

        TextArea ta = new TextArea();
        ta.setPrefWidth(400d);
        ta.setWrapText(true);

        vBox.getChildren().addAll(b1, b2, b3, b4, b5, ta);

        AnchorPane.setTopAnchor(vBox, 10d);
        AnchorPane.setLeftAnchor(vBox, 20d);
        an.getChildren().add(vBox);
        primaryStage.setScene(scene);
        primaryStage.setTitle("FileChooser");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();

        b1.setOnAction(event -> {

            Stage stage = new Stage();
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("单选文件");
            fileChooser.setInitialDirectory(new File("D:" + File.separator + "log"));
            // 过滤可以选择的文件类型
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.png"),
                    new FileChooser.ExtensionFilter("文本类型", "*.txt"),
                    new FileChooser.ExtensionFilter("所有类型", "*.*")
            );
            // 选择一个文件，关闭时会返回文件对象
            File file = fileChooser.showOpenDialog(stage);
            if (null != file) {
                System.out.println(file.getAbsoluteFile());
            }
        });

        b2.setOnAction(event -> {

            Stage stage = new Stage();
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("单选文件");
            fileChooser.setInitialDirectory(new File("D:" + File.separator + "log"));
            // 过滤可以选择的文件类型
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.png"),
                    new FileChooser.ExtensionFilter("文本类型", "*.txt"),
                    new FileChooser.ExtensionFilter("所有类型", "*.*")
            );
            // 选择多个文件，关闭时会返回文件对象
            List<File> files = fileChooser.showOpenMultipleDialog(stage);

            if (null != files && !files.isEmpty()) {
                files.forEach(file -> System.out.println(file.getAbsoluteFile()));
            }
        });

        b3.setOnAction(event -> {

            Stage stage = new Stage();
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("单选文件");
            fileChooser.setInitialDirectory(new File("D:" + File.separator + "log"));
            // 过滤可以选择的文件类型
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.png"),
                    new FileChooser.ExtensionFilter("文本类型", "*.txt"),
                    new FileChooser.ExtensionFilter("所有类型", "*.*")
            );
            // 选择一个文件，关闭时会返回文件对象
            File file = fileChooser.showOpenDialog(stage);
            if (null != file) {
                System.out.println(file.getAbsoluteFile());

                try {
                    FileInputStream fis = new FileInputStream(file);
                    InputStreamReader reader = new InputStreamReader(fis, "GBK");
//                    FileReader fr = new FileReader(file);
                    BufferedReader br = new BufferedReader(reader);
                    String str = null;
                    while ((str = br.readLine()) != null) {
                        ta.appendText(str);
                    }

//                    fr.close();
                    br.close();
                    reader.close();
                    fis.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        b4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage stage = new Stage();
                FileChooser fileChooser = new FileChooser();
                fileChooser.setTitle("单选文件");
                fileChooser.setInitialDirectory(new File("D:" + File.separator + "log"));
                // 过滤可以选择的文件类型
                fileChooser.getExtensionFilters().addAll(
                        new FileChooser.ExtensionFilter("文本类型", "*.txt"),
                        new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.png"),
                        new FileChooser.ExtensionFilter("所有类型", "*.*")
                );
                // 选择一个文件，关闭时会返回文件对象
                File file = fileChooser.showSaveDialog(stage);
                if (null != file) {
                    System.out.println(file.getAbsoluteFile());

                    try {
                        FileOutputStream fos = new FileOutputStream(file);

                        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
                        osw.write(ta.getText());
                        osw.flush();

                        osw.close();
                        fos.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        b5.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage stage = new Stage();
                DirectoryChooser directoryChooser = new DirectoryChooser();

                directoryChooser.setTitle("文件夹选择器");
                directoryChooser.setInitialDirectory(new File("D:" + File.separator + "log"));
                File file = directoryChooser.showDialog(stage);
                if (null != file) {
                    System.out.println(file.getAbsoluteFile());
                    if (file.isDirectory()) {
                        File[] files = file.listFiles();
                        for (File file1 : files) {
                            System.out.println(file1.getAbsoluteFile());
                        }
                    }
                }
            }
        });

    }
}
